﻿/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: Wireless
* Version: 0.2
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["wireless"] = new Object();
myPlugins["wireless"]["name"] = getLocalizedString2("mod_sysm_plugin_wireless_name");
myPlugins["wireless"]["onlyonce"] = true;
myPlugins["wireless"]["newObject"] = function (NewSettings) { return new plugin_wireless_sensor(NewSettings); }

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_wireless_sensor(NewSettings) {
	// Required variables
	this.plugin = "wireless";
	this.settings = NewSettings;
	this.edit = plugin_wireless_edit;
	this.ReDraw = plugin_wireless_ReDraw;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;


	//Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.barcolor_high) this.settings.barcolor_high = "#00FF00";
	if(!this.settings.barcolor_normal) this.settings.barcolor_normal = "#008000";
	if(!this.settings.barcolor_low) this.settings.barcolor_low = "#FF7F00";
	if(!this.settings.barcolor_offline) this.settings.barcolor_offline = "#FF0000";
	if(!this.settings.updspeed) this.settings.updspeed = 10;


	//The Sonsore
	this.wireless = new class_PercentBar(this.frame,0,0,getLocalizedString2("mod_sysm_plugin_wireless_label"));
	this.wireless.PercentColor(this.settings.color);


	// Update timer
	var obj = this;
	this.timer = new Timer();
	this.timer.interval = this.settings.updspeed;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_wireless_update.call(obj); };
	plugin_wireless_update.call(obj);


	// Calculating sensore height (required!)
	this.frame.height = this.wireless.background.vOffset + this.wireless.background.height;
	return true;

// --------------------------------------------------------------------------------
// Helper Functions

	function plugin_wireless_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_wireless_pref_color_high");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor_high;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_wireless_pref_color_normal");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor_normal;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_wireless_pref_color_low");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor_low;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_wireless_pref_color_offline");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor_offline;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_updatespeed_label");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(1,2,3,5,10,15,20);
		Drive_Edit_DialogFields[FieldID].option = new Array();
		for(var i = 0; i < Drive_Edit_DialogFields[FieldID].optionValue.length ; i++ )
			Drive_Edit_DialogFields[FieldID].option[i] = getLocalizedString2("mod_sysm_plugin_updatespeed_" + Drive_Edit_DialogFields[FieldID].optionValue[i]);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.updspeed;


		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.settings.barcolor_high = Drive_Edit_DialogResults[1];
			this.settings.barcolor_normal = Drive_Edit_DialogResults[2];
			this.settings.barcolor_low = Drive_Edit_DialogResults[3];
			this.settings.barcolor_offline = Drive_Edit_DialogResults[4];

			if ( this.settings.updspeed != Drive_Edit_DialogResults[5] ) {
				this.settings.updspeed = Drive_Edit_DialogResults[5];
				this.timer.interval = this.settings.updspeed;
			}

			this.wireless.PercentColor(this.settings.color);
			plugin_wireless_update.call(this);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}

	var theSignal = 0; // Namespace Global to save performance.
	var signalDisplay = "";
	function plugin_wireless_update() {
		if(system.wireless.powered && system.wireless.available) {
			this.wireless.text_name.data = system.wireless.network;

			// Addition by g6auc
			theSignal = system.wireless.signal;
			if ( system.platform == "macintosh" ) {
				theSignal -= 65536;
				signalDisplay = theSignal + ' dBm';
				theSignal+=90;
				if (theSignal > 55) theSignal = 55;
				if (theSignal < 0) theSignal = 0;
				theSignal = Math.round(100*theSignal/55);
			} else {
				if (theSignal > 70) theSignal = 70;
				theSignal = Math.round(100*theSignal/70);
				//signalDisplay = '';
			}

			this.wireless.PercentBarValue(theSignal,signalDisplay);
			if(theSignal > 75)
				this.wireless.PercentBarColor(this.settings.barcolor_high);
			else if(theSignal < 30)
				this.wireless.PercentBarColor(this.settings.barcolor_low);
			else
				this.wireless.PercentBarColor(this.settings.barcolor_normal);
		} else {
			this.wireless.text_name.data = "Wireless";
			this.wireless.PercentBarValue(100,getLocalizedString2("mod_sysm_plugin_offline"));
			this.wireless.PercentBarColor(this.settings.barcolor_offline);
		}
		return true;
	}

	function plugin_wireless_ReDraw() {
		this.frame.width = mod_sysmonitor_width;
		this.wireless.style = preferences.mod_sysmonitor_bar_style.value;
		this.wireless.size = mod_sysmonitor_size;
		this.frame.height = this.wireless.background.vOffset + this.wireless.background.height;
		return true;
	}

}

// --------------------------------------------------------------------------------
// The END